using System;
using System.Data;
using PCSComUtils.PCSExc;
using PCSComUtils.Common;
using System.Collections;
using PCSComProduct.Costing.DS;

namespace PCSComProduct.Items.BO
{
	public class ItemHourDetailBO //:  IItemHourDetailBO
	{
		private const string THIS = "PCSComProduct.Items.BO.ItemHourDetailBO";
		public ItemHourDetailBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}


	
		public void Add(object pObjectDetail)
		{
			// TODO:  Add ItemHourDetailBO.Add implementation

		}
	
		public void Delete(object pObjectVO)
		{
			// TODO:  Add ItemHourDetailBO.Delete implementation

		}
	
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add ItemHourDetailBO.GetObjectVO implementation
			return null;
		}
	
		public void Update(object pObjectDetail)
		{
			// TODO:  Add ItemHourDetailBO.Update implementation

		}
	
		public void UpdateDataSet(DataSet dstData)
		{
			// TODO:  Add ItemHourDetailBO.UpdateDataSet implementation

		}
		#region IItemHourDetailBO Members
		//**************************************************************************              
		///    <Description>
		///    Get the Cost information
		///    1.Cost Description
		///    2. and the order cost
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///       Button will be disable or enable
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       THIENHD
		///    </Authors>
		///    <History>
		///       15-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]

		public DataTable GetListOfCostDetail(int pintProductID, int pintCCNID)
		{
			try 
			{
				//1.Get the ITM_CostDescription
				ITM_CostDescriptionDS objITM_CostDescriptionDS = new ITM_CostDescriptionDS();
				DataTable dtCost_Description = objITM_CostDescriptionDS.GetHourCostDescription(pintCCNID);
				ArrayList arrDescription = new ArrayList();
				// sort description and change layout from horizontal to vertical
				foreach (DataColumn dcolData in dtCost_Description.Columns)
				{
					arrDescription.Add(dtCost_Description.Rows[0][dcolData.ColumnName].ToString().Trim());
				}
				arrDescription.TrimToSize();
				arrDescription.Sort();
				// get the order number of column name
				ArrayList arrColsOrdinal = new ArrayList();
				foreach (string strValue in arrDescription)
				{
					foreach (DataColumn dcolData in dtCost_Description.Columns)
					{
						if (dtCost_Description.Rows[0][dcolData.ColumnName].ToString().Trim() == strValue)
							arrColsOrdinal.Add(dcolData.ColumnName);
					}
				}
				arrColsOrdinal.TrimToSize();

				//2.Get the ITM_CostOperation
				ITM_CostDS objITM_CostDS = new ITM_CostDS();
				DataTable dtCostOperation = objITM_CostDS.GetHourCostOperation(pintProductID,pintCCNID);

				//3.Define the column name for this table
				const string COSTDESCRIPTION_FLD = "CostDescription";
				const string COMPONENTCOST_FLD = "ComponentHour";
				const string VALUEADDEDCOST_FLD = "ValueAddedHour";
				const string ITEMCOST_FLD = "OperationHour";


				//Create this table
				DataTable dtResult = new DataTable();
				
				dtResult.Columns.Add(COSTDESCRIPTION_FLD,System.Type.GetType("System.String"));
				dtResult.Columns.Add(COMPONENTCOST_FLD,System.Type.GetType("System.Decimal"));
				dtResult.Columns.Add(VALUEADDEDCOST_FLD,System.Type.GetType("System.Decimal"));
				dtResult.Columns.Add(ITEMCOST_FLD,System.Type.GetType("System.Decimal"));
				
				DataRow drNewRow;

				//Add default 21 empty row
				for (int i=1 ; i<=4; i++)
				{
					drNewRow = dtResult.NewRow();
					dtResult.Rows.Add(drNewRow);
				}

				bool blnHaveData = false;

				//first add the Cost Description to the 4 rows
				if (arrDescription.Count > 0)
				{
					blnHaveData = true;

					dtResult.Rows[0][COSTDESCRIPTION_FLD] = arrDescription[0];
					dtResult.Rows[1][COSTDESCRIPTION_FLD] = arrDescription[1];
					dtResult.Rows[2][COSTDESCRIPTION_FLD] = arrDescription[2];
					dtResult.Rows[3][COSTDESCRIPTION_FLD] = arrDescription[3];
				}

				//add the component cost, valued added cost, item cost
				if (dtCostOperation.Rows.Count > 0) 
				{
					blnHaveData = true;

					//assign the Component Cost
					for (int i = 0; i < arrColsOrdinal.Count; i++)
					{
						// get the colum name
						string strComponentCol = string.Empty;
						string strValueAddedCol = string.Empty;
						string strItemCostCol = string.Empty;
						switch (arrColsOrdinal[i].ToString())
						{
							case ITM_CostDescriptionTable.MACHRUNHRSDESC_FLD:
								strComponentCol = ITM_CostTable.COMPONENTMACHINERUNHOUR_FLD;
								strValueAddedCol = ITM_CostTable.VALUEADDEDMACHINERUNHOUR_FLD;
								strItemCostCol = ITM_CostTable.ITEMMACHINERUNHOUR_FLD;
								break;
							case ITM_CostDescriptionTable.MACHSETUPHRSDESC_FLD:
								strComponentCol = ITM_CostTable.COMPONENTMACHINESETUPHOUR_FLD;
								strValueAddedCol = ITM_CostTable.VALUEADDEDMACHINESETUPHOUR_FLD;
								strItemCostCol = ITM_CostTable.ITEMMACHINESETUPHOUR_FLD;
								break;
							case ITM_CostDescriptionTable.LABORRUNHRSDESC_FLD:
								strComponentCol = ITM_CostTable.COMPONENTLABORRUNHOUR_FLD;
								strValueAddedCol = ITM_CostTable.VALUEADDEDLABORRUNHOUR_FLD;
								strItemCostCol = ITM_CostTable.ITEMLABORRUNHOUR_FLD;
								break;
							case ITM_CostDescriptionTable.LABORSETUPHRSDESC_FLD:
								strComponentCol = ITM_CostTable.COMPONENTLABORSETUPHOUR_FLD;
								strValueAddedCol = ITM_CostTable.VALUEADDEDLABORSETUPHOUR_FLD;
								strItemCostCol = ITM_CostTable.ITEMLABORSETUPHOUR_FLD;
								break;
						}
						// assign the component hours
						dtResult.Rows[i][COMPONENTCOST_FLD] = dtCostOperation.Rows[0][strComponentCol];
						//assign the Value Added Hours
						dtResult.Rows[i][VALUEADDEDCOST_FLD] = dtCostOperation.Rows[0][strValueAddedCol];
					
						//assign the operation hour
						dtResult.Rows[i][ITEMCOST_FLD] = dtCostOperation.Rows[0][strItemCostCol];
					}
				}
				if (!blnHaveData)
				{
					dtResult.Rows.Clear();
				}
				
				
				return dtResult;
			}
			catch (PCSDBException ex) 
			{
				throw ex;
			}
			catch (Exception ex) 
			{
				throw ex;
			}
		}

		#endregion
	}
}
